import 'package:data/repository/home_articles/home_articles_repository.dart';
import 'package:data/repository/home_articles/home_articles_repository_impl.dart';
import 'package:dio/dio.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

import '../util/test_json_loader.dart';

class MockDio extends Mock implements Dio {}

void main() {
  group('test home articles repository', () {
    late HomeArticlesRepository repo;
    late MockDio mockDio;
    late Map<String, dynamic> testData;

    setUp(() {
      mockDio = MockDio();
      repo = HomeArticlesRepositoryImpl(dio: mockDio);
      testData = TestJsonDataLoader.loadSingleJsonData('home_articles.json');
    });

    tearDown(() {
      reset(mockDio);
    });

    test('response should not be empty', () async {
      final mockResponse = Response(
        statusCode: 200,
        data: testData,
        requestOptions: RequestOptions(),
      );
      when(() => mockDio.get(any())).thenAnswer((_) async => mockResponse);
      final result = await repo.fetchHomeArticles();

      expect(result.isNotEmpty, true);
      expect(result.length, 2);
      expect(result[0].audit, 1);
      expect(result[0].courseId, 13);
      expect(result[0].superChapterName, '广场Tab');
    });
    test('should return empty list when exception occurs', () async {
      when(() => mockDio.get(any())).thenThrow(Exception(''));
      final result = await repo.fetchHomeArticles();
      expect(result.isEmpty, true);
    });
  });
}
